package com.lgx.mall.view.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lgx.common.exception.DataException;
import com.lgx.common.web.Pagination;
import com.lgx.mall.core.entity.Product;
import com.lgx.mall.core.service.IProductService;
import com.lgx.mall.core.vo.ProductVO;
import com.lgx.mall.view.dao.IWebPageDetailDao;
import com.lgx.mall.view.entity.WebPageDetail;
import com.lgx.mall.view.service.IWebPageDetailService;

@Service
public class WebPageDetailService implements IWebPageDetailService {

	@Autowired
	private IWebPageDetailDao webPageDetailDao;
	
	@Autowired
	private IProductService productService;
	
	public IProductService getProductService() {
		return productService;
	}

	public void setProductService(IProductService productService) {
		this.productService = productService;
	}

	public IWebPageDetailDao getWebPageDetailDao() {
		return webPageDetailDao;
	}

	public void setWebPageDetailDao(
			IWebPageDetailDao webPageDetailDao) {
		this.webPageDetailDao = webPageDetailDao;
	}

	@Transactional
	public WebPageDetail createWebPageDetail(WebPageDetail gate)
			throws DataException {	
		return this.webPageDetailDao.createWebPageDetail(gate);
	}

	@Transactional
	public WebPageDetail updateWebPageDetail(WebPageDetail gate)
			throws DataException {
		return this.webPageDetailDao.updateWebPageDetail(gate);
	}

	@Transactional
	public void deleteWebPageDetail(WebPageDetail gate)
			throws DataException {
		this.webPageDetailDao.deleteWebPageDetail(gate);
	}

	@Transactional
	public void deleteWebPageDetail(long id) throws DataException {
		this.webPageDetailDao.deleteWebPageDetail(id);
	}

	
	public WebPageDetail findWebPageDetailById(long id) {
		return this.webPageDetailDao.findWebPageDetailById(id);
	}

	public Pagination findWebPageDetailPagination(String sql,
			Object[] params, int pageSize, int pageNum) {	
		return this.webPageDetailDao.findWebPageDetailPagination(sql, params, pageSize, pageNum);
	}

	
	public Pagination findWebPageDetailPagination(Object[] params,
			int pageSize, int pageNum) {
		return this.webPageDetailDao.findWebPageDetailPagination(params, pageSize, pageNum);
	}

	
	public void deleteWebPageDetails(String[] ids) throws DataException {
		this.webPageDetailDao.deleteWebPageDetails(ids);
	}

	
	public List<WebPageDetail> findWebPageDetailByAreaAndCid(
			String areaNode, long categoryId) {
		return this.webPageDetailDao.findWebPageDetailByAreaAndCid(areaNode, categoryId);
	}


	public Pagination findWebPageDetailPaginationByAreaAndCid(
			String areaNode, long categoryId, int pageSize, int pageNum) {
		return this.webPageDetailDao.findWebPageDetailPaginationByAreaAndCid(areaNode, categoryId, pageSize, pageNum);
	}

	@Override
	public List<ProductVO> findProductVOByAreaAndCid(String areaNode,
			long categoryId) {
		List<WebPageDetail> details = this.findWebPageDetailByAreaAndCid(areaNode, categoryId);
		
		List<ProductVO> vos = new ArrayList<ProductVO>();
		
		for(WebPageDetail detail : details){
			ProductVO vo = new ProductVO();
			vo.setEntityId(detail.getEntityId());
			vo.setEntityName(detail.getEntityName());
			vo.setUrl(detail.getEntityUrl());
			vo.setImageUrl1(detail.getImageUrl1());
			vo.setImageUrl2(detail.getImageUrl2());
			vo.setType(detail.getType());
			if(detail.getType() == 0 && !StringUtils.isEmpty(detail.getEntityId())){
				Product p = productService.findProductById(Long.parseLong(detail.getEntityId()));
				if(p!=null) {
					vo.setPrice(p.getPrice());
					vo.setUrl("product/"+p.getId()+".htm");
				}
			}else if(detail.getType() == 1){
				if(null!=detail.getEntityId()) {
					vo.setUrl("search/b/"+detail.getEntityId()+".htm");
				}
			}else if(detail.getType() == 2){
				
			}else if(detail.getType() == 3){
				
			}
			
			vos.add(vo);
		}
		
		return vos;
	}

}
